//
//  MileSeletor.m
//  iphoneTravelTour
//
//  Created by chen jiedan on 13-1-8.
//  Copyright (c) 2013年 Baidu. All rights reserved.
//

#import "MileSeletor.h"
#import "UIColor-Expanded.h"

@implementation MileSeletor{
    id<MileSeletorDelegate> _delegate;
}

@synthesize delegate = _delegate;


#define kDefaultMileSelect 0

- (id)initWithFrame:(CGRect)frame andDataSource:(NSArray *) distanceArray
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIImage *img = [UIImage imageNamed:@"mileSeletor-bg-gls"];
        UIImageView *bg = [[UIImageView alloc] initWithImage:[img stretchableImageWithLeftCapWidth:2 topCapHeight:25]];
        bg.frame = self.bounds;
        [self addSubview:bg];
        [bg release];
        
        NSInteger totalSize = [distanceArray count];
        for (int i = 0; i < totalSize; i++) {
            if (i < totalSize -1) {
                UIImage *line = [UIImage imageNamed:@"line-gls"];
                UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(10+64*i+26, 7, 51, 28)];
                lineView.image = [line stretchableImageWithLeftCapWidth:10 topCapHeight:4];
                [self addSubview:lineView];
                [lineView release];
            }
            
            UIButton *t = [UIButton buttonWithType:UIButtonTypeCustom];
            UILabel  *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(13+64*i, 24, 64, 30)];
            [noteLabel setBackgroundColor:[UIColor clearColor]];
            noteLabel.textColor = [UIColor colorWithHex:0xFF666666];
            noteLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
            UIImage *image = nil;
            noteLabel.text = [distanceArray objectAtIndex:i];

            if (i == 0) {
                image = [UIImage imageNamed:@"ic-yuan-left"];
            }else if (i == totalSize -1){
                image = [UIImage imageNamed:@"ic-yuan-right"];
            }else{
                image = [UIImage imageNamed:@"ic-yuan-center"];
            }
            
            UIImage *highLightImg = [UIImage imageNamed:@"ic-yuan-focused"];
            [t setImage:image forState:UIControlStateNormal];
            [t setImage:highLightImg forState:UIControlStateHighlighted];
            t.frame = CGRectMake(10+64*i, 0, 40, 44);
//            [t setBackgroundColor:[UIColor yellowColor]];
            t.tag = i +10;
            noteLabel.tag = i+20;
            [t addTarget:self action:@selector(mileBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:t];
            [self addSubview:noteLabel];
            [noteLabel release];
            
            
        }
       
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) mileBtnClick:(id)sender
{
    for (int i = 0; i  < 5; i++) {
        UIButton *btn = (UIButton*)[self viewWithTag:i+10];
        UILabel *label = (UILabel *)[self viewWithTag:i+20];
        if (btn != nil) {
            UIImage *image = nil;
            if (i == 0) {
                image = [UIImage imageNamed:@"ic-yuan-left"];
            }else if (i == 4){
                image = [UIImage imageNamed:@"ic-yuan-right"];
            }else{
                image = [UIImage imageNamed:@"ic-yuan-center"];
            }
            UIImage *highLightImg = [UIImage imageNamed:@"ic-yuan-focused"];

            [btn setImage:image forState:UIControlStateNormal];
            [btn setImage:highLightImg forState:UIControlStateHighlighted];
        }
        if (label != nil) {
            label.textColor = [UIColor colorWithHex:0xFF666666];
            label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
        }
    }
    
    UIButton *t = (UIButton *)sender;
    UIImage *image = nil;
    NSInteger tagNum = t.tag+10;
    UILabel *pressLabel = (UILabel *)[self viewWithTag:tagNum];
    pressLabel.textColor = [UIColor colorWithHex:0xFF0094FE];
    pressLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16];
    image = [UIImage imageNamed:@"ic-yuan-foused"];
    [t setImage:image forState:UIControlStateNormal];
    
    if ([_delegate respondsToSelector:@selector(didSelectMileSeletorAtIndex:)]) {
//        NSInteger level = 16 - (t.tag -10);
        [_delegate didSelectMileSeletorAtIndex:(t.tag -10)];
    }
    
}

/*set default btn clicked state*/
- (void) setDefaultBtnClick:(NSInteger)index
{
    UIButton *btn = (UIButton*)[self viewWithTag:index+10];
    [self mileBtnClick:btn];
    
}

- (void) showInView:(UIView *)view
{
    [view addSubview:self];
}
@end
